package com.ruoyi.wechat.service.impl;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.wechat.domain.ProductLeaseOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.wechat.mapper.CLeaseorderMapper;
import com.ruoyi.wechat.domain.CLeaseorder;
import com.ruoyi.wechat.service.ICLeaseorderService;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 租赁订单Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-25
 */
@Service
public class CLeaseorderServiceImpl implements ICLeaseorderService 
{
    @Autowired
    private CLeaseorderMapper cLeaseorderMapper;


    /**
     * 查询租赁订单
     * 
     * @param orderId 租赁订单主键
     * @return 租赁订单
     */
    @Override
    public CLeaseorder selectCLeaseorderByOrderId(String orderId)
    {
        return cLeaseorderMapper.selectCLeaseorderByOrderId(orderId);
    }

    /**
     * 查询租赁订单列表
     * 
     * @param cLeaseorder 租赁订单
     * @return 租赁订单
     */
    @Override
    public List<CLeaseorder> selectCLeaseorderList(CLeaseorder cLeaseorder)
    {
        return cLeaseorderMapper.selectCLeaseorderList(cLeaseorder);
    }

    @Override
    public List<CLeaseorder> getCLeaseorderByUserId(String userId, String orderId) {
        CLeaseorder cLeaseorder = new CLeaseorder();
        cLeaseorder.setOrderId(orderId);
        cLeaseorder.setUserId(userId);
        return cLeaseorderMapper.getCLeaseorderByUserId(cLeaseorder);
    }

    @Override
    public List<CLeaseorder> getCLeaseorderByUserIdList(String userId) {
        return cLeaseorderMapper.selectCLeaseorderByOrderIdList(userId);
    }

    /**
     * 新增租赁订单
     * 
     * @param cLeaseorder 租赁订单
     * @return 结果
     */
    @Override
    public int insertCLeaseorder(CLeaseorder cLeaseorder)
    {
        String orderId = IdUtils.randomUUID();
        // 给orderId设置UUID
        cLeaseorder.setOrderId(orderId);

        // 设置时间
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedDateTime = now.format(formatter);

        // 将格式化后的字符串解析回LocalDateTime对象（这一步如果前面已经有LocalDateTime对象可省略）
        LocalDateTime parsedDateTime = LocalDateTime.parse(formattedDateTime, formatter);

        // 将LocalDateTime转换为Date类型
        Date date = Date.from(parsedDateTime.atZone(ZoneId.systemDefault()).toInstant());
        cLeaseorder.setLeaseOrderTime(date);

        //给cartList设置值
        ArrayList<ProductLeaseOrder> cartList = cLeaseorder.getCartList();
        if (cartList!= null &&!cartList.isEmpty()) {
            // 将生成的orderId设置到cartList中的每个元素上
            for (ProductLeaseOrder productLeaseOrder : cartList) {
                productLeaseOrder.setOrderId(orderId);
            }
        }
        // 将 orderId 设置为二维码
        cLeaseorder.setQrCode(orderId);
        return cLeaseorderMapper.insertCLeaseorder(cLeaseorder);
    }

    /**
     * 新增商品订单
     *
     * @param cLeaseorder 商品订单
     * @return 结果
     */
    @Override
    public String insertProductOrder(CLeaseorder cLeaseorder)
    {
        String orderId = IdUtils.randomUUID();
        ArrayList<ProductLeaseOrder> cartList = cLeaseorder.getCartList();
        if (cartList!= null &&!cartList.isEmpty()) {
            // 将生成的orderId设置到cartList中的每个元素上
            for (ProductLeaseOrder productLeaseOrder : cartList) {
                productLeaseOrder.setOrderId(orderId);
            }
        }
        // 调用 新增商品订单方法
        this.insertCLeaseorder(cLeaseorder);
        cLeaseorderMapper.insertProductOrder(cLeaseorder);
        return cLeaseorder.getOrderId();
    }

    /**
     * 修改租赁订单
     * 
     * @param cLeaseorder 租赁订单
     * @return 结果
     */
    @Override
    public int updateCLeaseorder(CLeaseorder cLeaseorder)
    {
        return cLeaseorderMapper.updateCLeaseorder(cLeaseorder);
    }

    /**
     * 批量删除租赁订单
     * 
     * @param orderIds 需要删除的租赁订单主键
     * @return 结果
     */
    @Override
    public int deleteCLeaseorderByOrderIds(String[] orderIds)
    {
        return cLeaseorderMapper.deleteCLeaseorderByOrderIds(orderIds);
    }

    /**
     * 删除租赁订单信息
     *
     * @param orderId 租赁订单主键
     * @return 结果
     */
    @Override
    public int deleteCLeaseorderByOrderId(String orderId)
    {
        cLeaseorderMapper.deleteCLeaseorderByoId(orderId);
        return cLeaseorderMapper.deleteCLeaseorderByOrderId(orderId);
    }

    /**
     * 删除租赁订单中的商品商品
     * 
     * @param oId 租赁订单主键
     * @return 结果
     */
    @Override
    public int deleteCLeaseorderByoId(String oId)
    {
        return cLeaseorderMapper.deleteCLeaseorderByoId(oId);
    }
}
